﻿using Common;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Model;
using BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace AdminWeb.Filter
{
    /// <summary>
    /// 同步请求登录验证
    /// </summary>
    public class CheckAdminLogin : ActionFilterAttribute
    {

        //顾名思义就是指当要执行一个Action之前要做啥工作.在CheckLoginAttribute继承主类后,要将该OnActionExecuting(..)方法重写如:
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            string adminuser = filterContext.HttpContext.Session.GetString("adminuser");
            if (string.IsNullOrWhiteSpace(adminuser))
            {

                //filterContext.Result = new RedirectToRouteResult("Default", new Microsoft.AspNetCore.Routing.RouteValueDictionary());
                //根据cookie值 自动登录
                string adminuser_cookie = filterContext.HttpContext.Request.Cookies["adminuser_e"];
                if (string.IsNullOrWhiteSpace(adminuser_cookie))
                {
                    filterContext.Result = new RedirectToRouteResult("area", new Microsoft.AspNetCore.Routing.RouteValueDictionary(new { controller = "Home", action = "Login", area = "Admin" }));
                }
                else
                {
                    MemberBLL bll = new MemberBLL();
                    ReturnMessages rm = bll.Login(adminuser_cookie);
                    if (rm.code == 200)
                    {
                        filterContext.HttpContext.Session.SetString("adminuser", JSONHelper.SerializeObject(rm.results));
                    }
                    else
                    {
                        filterContext.Result = new RedirectToRouteResult("area", new Microsoft.AspNetCore.Routing.RouteValueDictionary(new { controller = "Home", action = "Login", area = "Admin" }));
                    }
                }
            }
            else
            {
                Member user = JSONHelper.DeserializeJsonToObject<Member>(adminuser);
                if (user == null)
                {
                    // 用户角色不对
                    //filterContext.Result = new RedirectToRouteResult("Default", new Microsoft.AspNetCore.Routing.RouteValueDictionary());
                    filterContext.Result = new RedirectToRouteResult("area", new Microsoft.AspNetCore.Routing.RouteValueDictionary(new { controller = "Home", action = "Login",area="Admin" }));
                }
            }
        }
    }

    /// <summary>
    /// 异步请求登录验证
    /// </summary>
    public class CheckAdminLoginAjax : ActionFilterAttribute
    {

        //顾名思义就是指当要执行一个Action之前要做啥工作.在CheckLoginAttribute继承主类后,要将该OnActionExecuting(..)方法重写如:
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            string adminuser = filterContext.HttpContext.Session.GetString("adminuser");
            if (string.IsNullOrWhiteSpace(adminuser))
            {
                //根据cookie值 自动登录
                string adminuser_cookie = filterContext.HttpContext.Request.Cookies["adminuser_e"];
                if (string.IsNullOrWhiteSpace(adminuser_cookie))
                {
                    //抱歉,你不具有当前操作的权限！
                    filterContext.Result = new JsonResult(new ReturnMessages { code = 401, msg = "请重新登录!" });
                }
                else
                {
                    MemberBLL bll = new MemberBLL();
                    ReturnMessages rm = bll.Login(adminuser_cookie);
                    if (rm.code == 200)
                    {
                        filterContext.HttpContext.Session.SetString("adminuser", JSONHelper.SerializeObject(rm.results));
                    }
                    else
                    {
                        //抱歉,你不具有当前操作的权限！
                        filterContext.Result = new JsonResult(new ReturnMessages { code = 401, msg = "请重新登录!" });
                    } 
                }
            }
            else
            {
                Member user = JSONHelper.DeserializeJsonToObject<Member>(adminuser);
                if (user == null)
                {
                    // 用户角色不对
                    filterContext.Result = new JsonResult(new ReturnMessages { code = 401, msg = "请重新登录!" });
                }
            }

        }
    }

}
